import http from "@/axios/http";

interface MenuResponse {
    code: number;
    message?: string;
    data?: any;
}
const api= "/TMenuController/";
const url={
    findMenus : "findMenus",
    findAllMenus:"findAllMenus",
    select : "select",
    update : "update",
    insert : "insert",
    deleteById:"deleteById"
}
const handleResponse = (res: MenuResponse) => {
    if (res.code === 200) {
        return res.data;
    }
    throw new Error(res.message || 'Operation failed');
};

const handleError = (error: Error, operation: string) => {
    console.error(`Error during ${operation}:`, error);
    throw error;
};

const menu = {
    /**
     * 根据用户ID获取菜单
     * @param userId 用户ID
     */
    findMenus: (teacherName: string | number) => {
        return http.get(api+url.findMenus, { teacherName })
            .then(handleResponse)
            .catch(error => handleError(error, 'findMenus'));
    },

    /**
     * 查询菜单
     * @param params 查询参数
     */
    select: (params: any) => {
        return http.post(api+url.findAllMenus, params)
        .then((res) => {
            if (res.code == 200) {
              return res
            } else {
              throw new Error(res.message || 'FIND failed')
            }
          })
            .catch(error => handleError(error, 'select'));
    },


    /**
     * 更新菜单
     * @param params 更新参数
     */
    update: (params: any) => {
        return http.post(api+url.update, params)
            .then(handleResponse)
            .catch(error => handleError(error, 'update'));
    },

    /**
     * 新增菜单
     * @param params 新增参数
     */
    insert: (params: any) => {
        return http.post(api+url.insert, params)
            .then(handleResponse)
            .catch(error => handleError(error, 'insert'));
    },

    /**
     * 根据ID删除菜单
     * @param id 菜单ID
     */
    deleteById: (id: string | number) => {
        return http.get(api+url.deleteById, { id })
            .then(handleResponse)
            .catch(error => handleError(error, 'deleteById'));
    },

    
    /**
     * 根据ID修改菜单名
     * @param id 菜单ID
     */
    editMenu: (params: any) => {
        return http.put("/TMenuController/editMenu", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'UPDATE failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during UPDATE:', error);
            throw error;
        });
    },
    deleteMenu: (params: any) => {
        return http.delete("/TMenuController/deleteMenu", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'DELETE failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during DELETE:', error);
            throw error;
        });
    },
    insertMenu: (params: any) => {
        return http.post("/TMenuController/insertMenu", params).then(res=>{
            if(res.code == 200){
                return res;
            }else{
                // 登录失败，抛出错误以便外部catch捕获
                throw new Error(res.message || 'POST failed');
            }
        }).catch(error=>{
            // 统一处理错误
            console.error('Error during POST:', error);
            throw error;
        });
    },

};

export default menu;